package com.ruoyi.hospital.service.consultationInfo.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.domain.doctor.DocIncome;
import com.ruoyi.hospital.domain.doctor.DocScheduleDetail;
import com.ruoyi.hospital.domain.vo.OrderDetailVo;
import com.ruoyi.hospital.dto.HotDoctor;
import com.ruoyi.hospital.dto.Piedate;
import com.ruoyi.hospital.dto.consultation.ConsultationDetail;
import com.ruoyi.hospital.dto.consultation.ConsultationParam;
import com.ruoyi.hospital.dto.consultation.ConsultationToday;
import com.ruoyi.hospital.mapper.doctor.DocIncomeMapper;
import com.ruoyi.hospital.mapper.doctor.DocScheduleDetailMapper;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.consultationInfo.ConsultationInfoMapper;
import com.ruoyi.hospital.domain.consultationInfo.ConsultationInfo;
import com.ruoyi.hospital.service.consultationInfo.IConsultationInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 问诊信息Service业务层处理
 * 
 * @author wendao
 * @date 2024-08-20
 */
@Service
public class ConsultationInfoServiceImpl implements IConsultationInfoService 
{
    @Autowired
    private ConsultationInfoMapper consultationInfoMapper;

    @Autowired
    private DocScheduleDetailMapper docScheduleDetailMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DocIncomeMapper docIncomeMapper;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 查询问诊信息
     * 
     * @param id 问诊信息主键
     * @return 问诊信息
     */
    @Override
    public ConsultationInfo selectConsultationInfoById(Integer id)
    {
        return consultationInfoMapper.selectConsultationInfoById(id);
    }

    @Override
    public ConsultationDetail selectConsultationInfoById1(Integer id) {
        return consultationInfoMapper.selectConsultationInfoById1(id);
    }

    @Override
    public OrderDetailVo selectConsultationInfoFordocincome(Integer id) {
        return consultationInfoMapper.selectConsultationInfoFordocincome(id);
    }

    /**
     * 查询问诊信息列表
     * 
     * @param consultationInfo 问诊信息
     * @return 问诊信息
     */
    @Override
    public List<ConsultationInfo> selectConsultationInfoList(ConsultationInfo consultationInfo)
    {
        return consultationInfoMapper.selectConsultationInfoList(consultationInfo);
    }

    @Override
    public List<ConsultationInfo> selectConsultationInfoListApp(ConsultationInfo consultationInfo) {
        return consultationInfoMapper.selectConsultationInfoListApp(consultationInfo);
    }

    @Override
    public List<ConsultationInfo> selectConsultationInfoListAppMe(ConsultationInfo consultationInfo) {
        return consultationInfoMapper.selectConsultationInfoListAppMe(consultationInfo);
    }

    @Override
    public List<ConsultationInfo> selectConsultationInfoListLatest(ConsultationInfo consultationInfo) {
        return consultationInfoMapper.selectConsultationInfoListLatest(consultationInfo);
    }

    @Override
    public List<ConsultationToday> selectConsultationInfoListToday(ConsultationParam consultationParam) {
        return consultationInfoMapper.selectConsultationInfoListToday(consultationParam);
    }

    @Override
    public int selectConsultationInfoListNum(ConsultationInfo consultationInfo) {
        return consultationInfoMapper.selectConsultationInfoListNum(consultationInfo);
    }

    @Override
    public List<ConsultationInfo> selectConsultationInfoListByState(ConsultationInfo consultationInfo) {
        return consultationInfoMapper.selectConsultationInfoListByState(consultationInfo);
    }

    /**
     * 新增问诊信息
     * 
     * @param consultationInfo 问诊信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertConsultationInfo(ConsultationInfo consultationInfo)
    {
        consultationInfo.setCreateTime(DateUtils.getNowTimeSecond());
        //生成问诊单号lh
        consultationInfo.setConNo("C"+System.currentTimeMillis());

        int i =  consultationInfoMapper.insertConsultationInfo(consultationInfo);


        //更新预约数量
        DocScheduleDetail docScheduleDetail = docScheduleDetailMapper.selectDocScheduleDetailById(consultationInfo.getAppointTime());
        if(docScheduleDetail!=null){
            if(StringUtils.isNullOrZero(docScheduleDetail.getNumsed())){
                docScheduleDetail.setNumsed(1);
            }else{
                docScheduleDetail.setNumsed(docScheduleDetail.getNumsed()+1);
            }
            docScheduleDetailMapper.updateDocScheduleDetail(docScheduleDetail);
        }


        try {
            //将顶到存放到redis中
            String key = "order:expire:" + consultationInfo.getId();
            redisTemplate.opsForValue().set(key, consultationInfo.getId(), 15, TimeUnit.MINUTES);
        } catch (Exception e) {
        }
        return i;
    }

    /**
     * 修改问诊信息
     * 
     * @param consultationInfo 问诊信息
     * @return 结果
     */
    @Override
    public int updateConsultationInfo(ConsultationInfo consultationInfo)
    {
        consultationInfo.setUpdateTime(DateUtils.getNowTimeSecond());
        return consultationInfoMapper.updateConsultationInfo(consultationInfo);
    }


    /*
    * 处理支付的逻辑
    * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateConsultationInfoForpay(ConsultationInfo consultationInfo) {
        int i = 0;
        //1、更新此问诊的状态
        i = consultationInfoMapper.updateConsultationInfo(consultationInfo);

        //2、添加到医生的收入：状态为未支付

        ConsultationInfo consultationInfo1 = consultationInfoMapper.selectConsultationInfoById(consultationInfo.getId());

        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique("doc.con.commission");
        if(sysConfig!=null && sysConfig.getIsListed()==1){   //开启的时候

            DocIncome docIncome = docIncomeMapper.selectDocIncomeBy3(consultationInfo1.getDocId(), consultationInfo1.getConType(), consultationInfo1.getId());
            if(docIncome==null){
                docIncome = new DocIncome();
                docIncome.setDocId(consultationInfo1.getDocId());
                docIncome.setIncomeStatus(0); //未结算
                docIncome.setIncomeType(consultationInfo1.getConType());
                docIncome.setOrderId(consultationInfo1.getId());
                docIncome.setOrderPrice(consultationInfo1.getConAmount());
                docIncome.setCreateTime(DateUtils.getNowTimeSecond());
                docIncome.setIncomeRate(sysConfig.getConfigValue());
                docIncome.setIncomeNum(consultationInfo1.getConAmount().multiply(new BigDecimal(sysConfig.getConfigValue())));
                //收入
                docIncomeMapper.insertDocIncome(docIncome);
            }


        }

        return i;
    }

    @Override
    public int endConsultationInfo(ConsultationInfo consultationInfo) {
        consultationInfo.setUpdateTime(DateUtils.getNowTimeSecond());

        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique("doc.con.commission");
        if(sysConfig!=null && sysConfig.getIsListed()==1){   //开启的时候
            DocIncome docIncome = docIncomeMapper.selectDocIncomeBy3(consultationInfo.getDocId(), consultationInfo.getConType(), consultationInfo.getId());
            if(docIncome==null){
                docIncome = new DocIncome();
                docIncome.setDocId(consultationInfo.getDocId());
                docIncome.setIncomeStatus(1); //结算
                docIncome.setIncomeType(consultationInfo.getConType());
                docIncome.setOrderId(consultationInfo.getId());
                docIncome.setOrderPrice(consultationInfo.getConAmount());
                docIncome.setCreateTime(DateUtils.getNowTimeSecond());
                docIncome.setIncomeRate(sysConfig.getConfigValue());
                docIncome.setIncomeNum(consultationInfo.getConAmount().multiply(new BigDecimal(sysConfig.getConfigValue())));
                //收入
                docIncomeMapper.insertDocIncome(docIncome);
            }else{
                docIncome.setIncomeStatus(1); //结算
                docIncomeMapper.updateDocIncome(docIncome);
            }
        }
        return consultationInfoMapper.updateConsultationInfo(consultationInfo);
    }

    /**
     * 批量删除问诊信息
     * 
     * @param ids 需要删除的问诊信息主键
     * @return 结果
     */
    @Override
    public int deleteConsultationInfoByIds(Integer[] ids)
    {
        return consultationInfoMapper.deleteConsultationInfoByIds(ids);
    }

    /**
     * 删除问诊信息信息
     * 
     * @param id 问诊信息主键
     * @return 结果
     */
    @Override
    public int deleteConsultationInfoById(Integer id)
    {
        return consultationInfoMapper.deleteConsultationInfoById(id);
    }

    @Override
    public Double getNum() {
        return consultationInfoMapper.getNum();
    }

    @Override
    public List<Piedate> getPiedate() {
        return consultationInfoMapper.getPiedate();
    }

    @Override
    public List<HotDoctor> getHotdoctor() {
        return consultationInfoMapper.getHotdoctor();
    }

    @Override
    public int getConNum(int conState,int docId) {
        return consultationInfoMapper.getConNum(conState,docId);
    }


}
